﻿@model NK.Mvc3.Models.Sale
@{
    ViewBag.Title = "Grid";
}
<h2>Grid</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>


<fieldset>
    <legend>Sale</legend>

    <div class="display-label">DateSale</div>`
    <div class="display-field">
        @Html.DisplayFor(model => model.DateSale)
    </div>

    <div class="display-label">StaffName</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.StaffName)
    </div>
    <p>
        <div id="AjaxGridContainer">
            <p>
                <a href="#insertEmptyRow" class="insertEmptyRow hidden">Create New</a>
            </p>
            <table id="AjaxGrid">
                <thead>
                    <tr>
                        <th>
                            BarCode
                        </th>
                        <th>
                            POS
                        </th>
                        <th>
                            Quantity
                        </th>
                        <th>
                            CostSale
                        </th>
                        <th>
                            TotalCost
                        </th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @{Html.RenderAction("GridData", new { id = Model.ID });}
                </tbody>
            </table>
        </div>
    </p>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

@* ReSharper disable AssignedValueIsNeverUsed *@
<script>
    $(function () {
        // alert("Init");
        var gridcontainer, gridContext;
        var unloadWarningSet;

        $.ajaxSetup({ cache: false });

        function setUnloadWarning() {
            if (!unloadWarningSet) {
                unloadWarningSet = true;
                $(window).bind('beforeunload', function () {
                    return 'You have unsaved changes. Are you sure you want to leave?';
                });
            }
        }
        function clearUnloadWarning() {
            //gridContext.find('td').filter(':last-child').removeClass('hidden');
            if (unloadWarningSet && $('.createRow, .editRow').length === 0) {
                unloadWarningSet = false;
                $(window).unbind('beforeunload');
            }
        }
        function createRow(row) {
            var postParams = getPostParams(row);
//            var form = $(row).find('form');
//            if (form == null) {
//                form = $(row).parents('form');
//            }
//            alert(form);
            //alert(postParams.g);
            $.post('@Html.Raw(Url.Action("CreateSaleDetail"))', postParams)
                .done(function (newRow) {
                    //alert('OK');
                    row.replaceWith(newRow);
                    clearUnloadWarning();
                })
                .fail(function () {
                    alert('false');
                    row.addClass('error');
                });
        }

        function getPostParams(row) {
            return row.find(':input[name]:not(:disabled)').filter(':not(:checkbox), :checked').map(function () {
                var input = $(this);
                if (input.validate()) {
                    return { name: input.attr('name'), value: input.val() };
                }
                return null;
            }).get();
        }

        $(function () {
            gridcontainer = $("#AjaxGridContainer");
            gridContext = gridcontainer.find("#AjaxGrid");
            gridcontainer.find('.insertEmptyRow').removeClass('hidden');

            $('.insertEmptyRow').click(function (e) {
                //alert("click");
                $.get('@Html.Raw(Url.Action("CreateSaleDetail", new {saleId = Model.ID}))', function (emptyRow) {
                    //alert("ok");
                    gridContext.find('tbody').prepend(emptyRow);
                    //emptyRow.attachValidation();
                    //$.validator.unobtrusive.parse('tbody form' );

                    //$jQval.unobtrusive.parse("#newEmptyRow");
                    //alert("ok");
                });
                setUnloadWarning();
                e.preventDefault();
            });
            gridContext.delegate('.createRow', 'click', function () {
                createRow($(this).parents('tr'));
            });
            gridContext.delegate('.cancelCreateRow', 'click', function () {
                $(this).parents('tr').remove();
                clearUnloadWarning();
            });
            gridContext.delegate('input[name="BarCode"]', 'change', function () {
                //alert('change');
                var barcodeValue = $(this).val();
                var posText = $(this).parents('tr').find('input[name="POSDesctiption"]');
                var productIdHidden = $(this).parents('tr').find('input[name="ProductInfoID"]');
                if (barcodeValue != null && barcodeValue != '') {
                    $.post('@Html.Raw(Url.Action("GetDetails","ProductInfo", new {barcode = "__BarCode__"}))'.replace("__BarCode__", barcodeValue), function (productInfo) {
                        $(posText).val(productInfo.POSDescription);
                        $(productIdHidden).val(productInfo.ID);
                    });
                }
            });
        });
    });

</script>
@* ReSharper restore AssignedValueIsNeverUsed *@
